<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_inclock/model')->方法名();
*/
namespace jy_inclock;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function checkCanPick($task, $mid)
	{
		if (empty($mid)) {
			$mid = $_SESSION['uid'];
		}
		if (empty($mid)) {
			return array('status'=>'-1','num'=>'1','message'=>'请先登录');
			
		}
	/*	if (substr($task['type'], 0, 7) == 'pyramid') {
			if (!p('commission')->isAgent($mid)) {
				return error(1, '只有分销商能接此任务');
			}
		}
		*/
		$time = time();
		if ($time < $task['starttime']) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务尚未开放');
		}
		if ($time < $task['endtime']) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务已结束');
		}
		$sql = 'select * from ' . tablename('jy_inclock_task_record') . ' where mid = :mid and taskid = :taskid and uniacid = :uniacid order by id desc';
		$lastRecord = pdo_fetch($sql, array(':mid' => $mid, ':taskid' => $task['id'], ':uniacid' => $_SESSION['uniacid']));
		if (empty($lastRecord)) {
			
			return array('status'=>'1','num'=>'','message'=>'');
		}
		if ($task['repeat_type'] == 1) {
			return array('status'=>'-1','num'=>'-1','message'=>'任务已结束');
			
		}
		$finishtime = strtotime($lastRecord['finishtime']);
		$stoptime = strtotime($lastRecord['stoptime']);
		if ($finishtime < 0 && ($time < $stoptime || $stoptime < 0)) {
		/*	if ($_W['ispost'] && !empty($_GPC['mid'])) {
				$this->send2wechat($lastRecord['id'], $_GPC['mid']);
			}
		*/
			return array('status'=>'-1','num'=>'-1','message'=>'任务未完成，不能重复领取');
		}
		if (0 < $finishtime) {
			$compareTime = $finishtime;
		}
		else {
			if ($time < $stoptime) {
				$compareTime = $stoptime;
			}
		}
		if ($task['repeat_type'] == 0) {
			return array('status'=>'1','num'=>'','message'=>'');
		}
		if ($task['repeat_type'] == 2) {
			if ($task['repeat_interval'] < $time - $compareTime) {
				return array('status'=>'1','num'=>'','message'=>'');
			}
		
			return array('status'=>'-1','num'=>'-1','message'=>$task['repeat_interval'] . '秒后才能再接此任务');
		}
		if ($task['repeat_type'] == 3) {
			if ($task['repeat_cycle'] == 1) {
				if (86400 < (int) strtotime(date('Ymd')) - (int) strtotime(date('Ymd', $compareTime))) {
					return array('status'=>'1','num'=>'','message'=>'');
				}
				return array('status'=>'-1','num'=>'-1','message'=>'明天才能再接此任务');
			}
			if ($task['repeat_cycle'] == 2) {
				$w = date('w', $compareTime);
				$w == 0 && ($w = 7);
				$between = diffBetweenTwoDays($compareTime, $time);
				if (7 < $w + $between) {
					return array('status'=>'1','num'=>'','message'=>'');
				}
				return array('status'=>'-1','num'=>'-1','message'=>'下个周才能再接此任务');
			}
			if ($task['repeat_cycle'] == 3) {
				if (0 < date('Ym') - date('Ym', $compareTime)) {
					return array('status'=>'1','num'=>'','message'=>'');
				}
				
				return array('status'=>'-1','num'=>'-1','message'=>'下个月才可以再接此任务');
			}
		}
		else {
			
			return array('status'=>'-1','num'=>'-1','message'=>'重复领取类型不详');
		}
		return array('status'=>'1','num'=>'','message'=>'');
	}
/**
     * 两天相差的天数
     */
 public function diffBetweenTwoDays($day1, $day2)
{
	$second1 = strtotime(date('Y-m-d', $day1));
	$second2 = strtotime(date('Y-m-d', $day2));
	if ($second1 < $second2) {
		$tmp = $second2;
		$second2 = $second1;
		$second1 = $tmp;
	}
	return ($second1 - $second2) / 86400;
}
public function stoptime($task)
{
	$time = time();
	$stoptime = '0000-00-00 00:00:00';
	if ($task['picktype'] == 1) {
		return $task['endtime'];
	}
	if ($task['stop_type'] == 1) {
		$stoptime = date('Y-m-d H:i:s', $time + $task['stop_limit']*3600);
	}
	else if ($task['stop_type'] == 2) {
		$stoptime = $task['stop_time'];
	}
	else {
		if ($task['stop_type'] == 3) {
			switch ($task['stop_cycle']) {
			case '0':
				$stoptime = date('Y-m-d 00:00:00', strtotime('+1 day'));
				break;
			case '1':
				$stoptime = date('Y-m-d 00:00:00', strtotime('next Monday'));
				break;
			case '2':
				$stoptime = date('Y-m-d 00:00:00', mktime(0, 0, 0, date('n') + 1, 1, date('Y')));
				break;
			}
	  	}
	}
	return $stoptime;
}
 /* task插件公用save方法,添加/编辑任务表
     * @param $table
     * @param $task
     * @param bool $Update 是否支持更新
     * @return bool
     */
public function taskSave($table, $task, $Update = true)
{
	$task['uniacid'] = intval($_SESSION['uniacid']);
	//$isIdKey = in_array('id',$table);
	$isIdKey = true;
	if (empty($task['id']) && $isIdKey) {
		pdo_insert($table, $task);
		return pdo_insertid();
	}
	if (!$isIdKey) {
		$countSql = 'select count(*) from ' . tablename($table) . ' where uniacid = :uniacid';
		$ifExist = pdo_fetchcolumn($countSql, array('uniacid' => $_SESSION['uniacid']));
		if ($ifExist && $Update) {
			pdo_update($table, $task, array('uniacid' => $_SESSION['uniacid']));
			if (empty($task['id'])) {
				return $_SESSION['uniacid'];
			}
			return $task['id'];
		}
		if (!$ifExist) {
			pdo_insert($table, $task);
			return pdo_insertid();
		}
		return false;
		}
		if ($Update) {
			pdo_update($table, $task, array('id' => $task['id']));
			if (empty($task['id'])) {
				return $_SESSION['uniacid'];
			}
			return $task['id'];
		}
		return false;
	}
	/**
     * 校验数据库字段
     * @param $task
     */
	public function checkDbFormat($table, &$task)
	{   
		if (!is_array($task) || !is_array($table)) {
			return $task = false;
		}
		$field = array_flip($table);
		$diff = array_diff_key($task, $field);
		if (!empty($diff)) {
			return $task = false;
		}
		foreach ($task as &$t) {
			if (is_array($t)) {
				$t = json_encode($t);
			}
		}
		return true;
	}
	public function getTaskType($type = '')
	{  
		$task_type= $this->task_types;
		if (empty($type)) {		
			return $task_type;
		}
		foreach ($task_type as $k=>$tasktype) {
			if ($type == $k) {
				return $tasktype;
			}
		}
		return false;
	}
	//检查任务是否完成
	public function checkTask($task_id){
		
	}
	//审核打卡
	public function checklog($logid,$status=0,$msg=''){
		$log=$this->get('task_record_log',array('id'=>$logid));
		if(!$log){
			return;
		}
		$update=array(
			'status'=>$status,
			'msg'=>$msg,
		);
		if($update['status']==1){
			//审核通过
			$record_update=array(
				'task_progress +='=>1,
			);
			$record=$this->get('task_record',array('id'=>$log['record_id']));
			if($record['task_progress']+1>=$record['task_demand']){
				$record_update['status']=1;
				$record_update['finishtime']=TIMESTAMP;
			}
			$this->update('task_record',$record_update,array('id'=>$log['record_id']));
		}else{
			$update['finishtime']=TIMESTAMP;
		}
		$this->update('task_record_log',$update,array('id'=>$logid));
		return true;
	}
	//刷新任务
	public function uprecord(){
		$record=$this->getall('task_record',array('status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		foreach($record as $item){
			$stoptime=($item['stoptime']=='0000-00-00 00:00:00')?0:strtotime($item['stoptime']);
			if($stoptime>0 && $stoptime<time()){
				$this->update('task_record',array('status'=>2),array('id'=>$item['id']));
			}
		}
		return true;
	}
	//完成任务
	public function checkfinish($record_id){
		$record=$this->get('task_record',array('id'=>$record_id));
		if(!$record){
			return;
		}
		if($record['status']==2){
			exi('任务已结束！','error');
		}		
		$task=$this->pdo_get('task_list',array('id'=>$record['taskid'],'uniacid'=>$_SESSION['uniacid']));
		if($record['finishtime'] && $task['cangettime'] && $record['finishtime']>=TIMESTAMP-($task['cangettime']*24*3600)){
			$record['lastgettime']=$record['finishtime']+($task['cangettime']*24*3600)-TIMESTAMP;
			$record['lastgettime']=$record['lastgettime']/(24*3600);
			$record['lastgettime']=price($record['lastgettime']);
			exi('还不能领奖哦，再等待'.$record['lastgettime'].'天。','error');
		}
		if($record['task_progress']>=$record['task_demand']){
			$isget=$this->get('task_reward',array('recordid'=>intval($record['id']),'mid'=>intval($record['mid']),'uniacid'=>$_SESSION['uniacid']));
			if($isget['id']){
				exi('您已领取奖励！不能再领取奖励！','error');
			}
			$reward_desc='奖励';
			$reward=json_decode($task['reward'],true);
			foreach($reward as $key=>$value){
				if(strpos($key,'credit') !== false && $value>0){
					$type=substr($key, -1);
					$reward_desc.='['.$value.$credit_types[$type].'] ';
					cfc('core')->member_credit($record['mid'],'credit'.$type,$value,1,'jy_inclock','打卡任务奖励：'.$task['title'],'',$_SESSION['uid']);
				}
			}
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'tasktitle'=>$task['title'],
				'tasktype'=>$task['type'],
				'taskid'=>intval($record['taskid']),
				'recordid'=>intval($record['id']),
				'mid'=>intval($record['mid']),
				'reward_desc'=>$reward_desc,
				'get'=>1,
				'sent'=>1,
				'gettime'=>TIMESTAMP,
				'senttime'=>TIMESTAMP,
			);
			$this->pdo_insert('task_reward',$insert);
			$this->update('task_record',array('status'=>1),array('id'=>$record_id));
		}else{
			exi('还未完成打卡任务！不能领取奖励！','error');
		}
		return true;
	}
}
?>